package com.ljm.sys.auth.controller;

import com.ljm.common.dto.BaseResult;
import com.ljm.common.util.ResultUtil;
import com.ljm.shiro.util.UserUtil;
import com.ljm.sys.auth.api.SysAuthRoleApi;
import com.ljm.sys.auth.api.SysAuthRolePermissionApi;
import com.ljm.sys.auth.dto.SysAuthRoleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author HuamingChen
 * @since 2018-12-19
 */
@Api(description="系统角色")
@Controller
@RequestMapping("/auth/sysRole")
public class SysRoleController {

    /**
     * 系统角色接口
     */
    @Resource
    private SysAuthRoleApi sysAuthRoleApi;
    /**
     * 角色权限关联api
     */
    @Resource
    private SysAuthRolePermissionApi sysAuthRolePermissionApi;

    @ApiOperation(value = "保存系统角色")
    @PostMapping(value = "/save")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role.add","sys:auth:role.edit"})
    public BaseResult save(@ApiParam(value = "系统角色") @RequestBody SysAuthRoleDto dto) {
        sysAuthRoleApi.saveData(UserUtil.getUserInfo(),dto,dto.getPermissionCodes());
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除系统角色")
    @GetMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role.del"})
    public BaseResult del(@ApiParam(value = "主键") @RequestParam Long id) {
        sysAuthRoleApi.del(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取系统角色")
    @GetMapping(value = "/getById")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role.view","sys:auth:role.edit"})
    public BaseResult getById(@ApiParam(value = "主键") @RequestParam Long id) {
        return ResultUtil.successData(sysAuthRoleApi.getDataById(id));
    }

    @ApiOperation(value = "分页获取系统角色")
    @GetMapping(value = "/getByPage")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role.list"})
    public BaseResult getByPage(
            @ApiParam(value = "角色名称") @RequestParam(required = false) String roleName,
            @ApiParam(value = "角色代码") @RequestParam(required = false) String roleCode,
            @ApiParam(value = "页码") @RequestParam Integer pageNo,
            @ApiParam(value = "页大小") @RequestParam Integer pageSize) {
        return ResultUtil.successData(sysAuthRoleApi.getDataByPage(roleName,roleCode,pageNo,pageSize));
    }

    @ApiOperation(value = "获取角色拥有的权限")
    @GetMapping(value = "/getPermissionByRoleCode")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role:authorize"})
    public BaseResult getPermissionByRoleCode(@RequestParam String roleCode){
        return ResultUtil.successData(sysAuthRolePermissionApi.getPermissionByRoleCode(roleCode));
    }

    @ApiOperation(value = "配置权限")
    @PostMapping(value = "/setSysPermission")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role:authorize"})
    public BaseResult setSysPermission(@RequestBody SysAuthRoleDto role){
        sysAuthRoleApi.setSysPermissions(role.getRoleCode(),role.getPermissionCodes(),UserUtil.getUserInfo());
        return ResultUtil.success();
    }

    @ApiOperation(value = "指派用户")
    @PostMapping(value = "/setUserRoleRef")
    @ResponseBody
    @RequiresPermissions({"sys:auth:role:appoint"})
    public BaseResult setUserRoleRef(@RequestBody SysAuthRoleDto role){
        sysAuthRoleApi.handleUserRef(role.getRoleCode(),role.getUserIds(),UserUtil.getUserInfo());
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping(value = "/getAllRole")
    @ResponseBody
    @RequiresPermissions({"org:user:add"})
    public BaseResult getAllRole(){
        return ResultUtil.successData(sysAuthRoleApi.getAllRole(UserUtil.getUserInfo()));
    }
}
